<?php

namespace App\Services\Tools;

use App\Enums\ContentNameEnum;
use App\Enums\GroupEnum;
use App\Jobs\Content\GroupMessageJob;
use App\Traits\Singleton;
use Illuminate\Support\Collection;

class GroupMessageAuditService
{
    use Singleton;
    /**
     * 推送群语音数据消息到队列
     *
     * @param int $messageId
     * @param int $userId
     * @param int $groupId
     * @param array $audio
     * @return void
     */
    public function audioAudit(int $messageId, int $userId, int $groupId, array $audio)
    {
        $data = [
            'key_id' => $messageId,
            'content_name'=>ContentNameEnum::GROUP_MESSAGE,
            'user_id' => $userId,
            'group_id' => $groupId,
            // 注意 url 中的转义问题
            'voices' => Collection::make($audio)->where('type', GroupEnum::MEDIA_TYPE_AUDIO)->pluck('url')->toJson(),
        ];
        $queueName = env('CONTENT_QUEUE_NAME');
        dispatch(new GroupMessageJob($data))->onQueue($queueName);
    }

    public function bulletinAudit(string $messageId, int $userId, int $groupId, array $bulletin)
    {
        $data = [
            'key_id' => $messageId,
            'user_id' => $userId,
            'group_id' => $groupId,
            'content' => $bulletin['content'],
            'pictures' => is_array($bulletin['image']) ? json_encode($bulletin['image']) : json_encode([$bulletin['image']]),
            'content_name'=>ContentNameEnum::GROUP_BULLETIN,
        ];
        $queueName = env('CONTENT_QUEUE_NAME');
        dispatch(new GroupMessageJob($data))->onQueue($queueName);
    }

    /**
     * 同城群派对创建
     * @param  int  $messageId
     * @param  int  $userId
     * @param  int  $groupId
     * @param  array  $bulletin
     *
     */
    public function cityGroupAudit(int $groupPartId, int $userId, int $groupId, array $params)
    {
        $data = [
            'key_id' => $groupPartId,
            'content_name'=>ContentNameEnum::GROUP_CITY_PARTY,
            'user_id' => $userId,
            'group_id' => $groupId,
            'content' =>json_encode([
                'title'=>$params['title'],
                'content'=>$params['content'],
            ]),
            'pictures' => is_array($params['cover']) ? json_encode($params['cover']) : json_encode([$params['cover']]),
        ];

        $queueName = env('CONTENT_QUEUE_NAME');
        dispatch(new GroupMessageJob($data))->onQueue($queueName);
    }

    /**
     * 推送群图片数据消息到队列
     *
     * @param int $messageId
     * @param int $userId
     * @param int $groupId
     * @param array $image
     * @return void
     */
    public function imageAudit(int $messageId, int $userId, int $groupId, array $image)
    {
        $data = [
            'key_id' => $messageId,
            'content_name'=>ContentNameEnum::GROUP_MESSAGE,
            'user_id' => $userId,
            'group_id' => $groupId,
            // 注意 url 中的转义问题
            'pictures' => Collection::make($image)->where('type', GroupEnum::MEDIA_TYPE_IMAGE)->pluck('url')->toJson(),
        ];
        $queueName = env('CONTENT_QUEUE_NAME');
        dispatch(new GroupMessageJob($data))->onQueue($queueName);
    }

    /**
     * 群成员昵称修改审核
     * @param  int  $messageId
     * @param  int  $userId
     * @param  int  $groupId
     * @param  array  $image
     */
    public function memberNicknameAudit(int $messageId, int $userId, int $groupId,$content)
    {
        $data = [
            'key_id' => $messageId,
            'content_name'=>ContentNameEnum::GROUP_MEMBER_NICKNAME,
            'user_id' => $userId,
            'group_id' => $groupId,
            'content' =>$content
        ];
        $queueName = env('CONTENT_QUEUE_NAME');
        dispatch(new GroupMessageJob($data))->onQueue($queueName);

    }


    /**
     * 推送群文字数据消息到队列
     *
     * @param int $messageId
     * @param int $userId
     * @param int $groupId
     * @param string $text 拼接好的文本
     * @return void
     */
    public function textAudit(int $messageId, int $userId, int $groupId, string $text)
    {
        $data = [
            'key_id' => $messageId,
            'content_name'=>ContentNameEnum::GROUP_MESSAGE,
            'user_id' => $userId,
            'group_id' => $groupId,
            'content' => $text,
        ];
        $queueName = env('CONTENT_QUEUE_NAME');
        dispatch(new GroupMessageJob($data))->onQueue($queueName);
    }

    /**
     * 推送群视频数据消息到队列
     *
     * @param int $messageId
     * @param int $userId
     * @param int $groupId
     * @param array $video
     * @return void
     */
    public function videoAudit(int $messageId, int $userId, int $groupId, array $video)
    {
        $data = [
            'key_id' => $messageId,
            'content_name'=>ContentNameEnum::GROUP_MESSAGE,
            'user_id' => $userId,
            'group_id' => $groupId,
            // 注意 url 中的转义问题
            'videos' => Collection::make($video)->where('type', GroupEnum::MEDIA_TYPE_VIDEO)->pluck('url')->toJson(),
        ];
        $queueName = env('CONTENT_QUEUE_NAME');
        dispatch(new GroupMessageJob($data))->onQueue($queueName);
    }
}